var _CONFIG = {
    tBINDING_URI: base_url + 'so/staff/binding',
    EDITPANEL_URI:base_url + 'so/staff/editpanel',
    COMMIT_URI:base_url + 'so/excution/commit/staff/staff/staf',
    DELETE_URI:base_url + 'so/excution/delete/staff/staff/staf'
};
var _SO = (function() {
    var oTable;
    var that = this;
    return {
        createtable :   function(){
             oTable = $('#data_table').dataTable({
                //aaSorting: [],
                aoColumns: [
                    {
                        mData: "staf_id",sWidth: "58px",sClass:'',
                        mRender: function ( value, type, datarow ) {
                            var str = "";
                            str += '<span class="icon-pencil" title="Edit:'+value+'" style="padding-right:12px" onclick="_SO.onEditItem(' + value + ');"></span>';
                            str += '<span class="icon-trash" title="Delete:'+value+'" style="padding-right:12px" onclick="_SO.onDeleteItem(' + value + ');"></span>';

                            return str;
                        }
                    },
                    {
                        mData: "staf_avatar",sWidth: "58px",
                        mRender: function ( value, type, datarow ) {
                            var str = "";
                            str += '<img src="' + value + '" style="height:40px"/>';
                            return str;
                        }
                    },
                    {
                        mData: "staf_name",
                        mRender: function ( value, type, datarow ) {
                            var str = "";
                            str += '<b>' + datarow.staf_name + '</b> - <i>' + datarow.staf_position + '</i>';
                            str += '<br/>Created: ' + datarow.staf_insert + '';
                            return str;
                        }
                    },
                    {mData: "staf_position",bVisible:false},
                    {
                        mData: "staf_status",sClass:'center',sWidth: "58px",
                        mRender: function ( value, type, datarow ) {
                            var str = "";
                            if(value==='true')
                                str+='<input type="checkbox" onclick="_SO.onStatusChange(this,'+datarow.staf_id+');" checked=1/>';
                            else
                                str+='<input type="checkbox" onclick="_SO.onStatusChange(this,'+datarow.staf_id+');"/>';
                            return str;
                        }
                    },
                    {mData: "staf_insert",bVisible:false},
                    {mData: "staf_update",bVisible:false}
                ],
                sServerMethod: "POST",
                iDisplayLength: 50,
                bProcessing: true,
                bServerSide: true,
                bPaginate: false,
                sAjaxSource: _CONFIG.tBINDING_URI,
                bJQueryUI: false,
                bAutoWidth: false,
                sPaginationType: "full_numbers",
                //sDom": '<"datatable-header"fl>t<"datatable-footer"ip>',
                oLanguage: {
                    sSearch: "<span>Filter records:</span> _INPUT_",
                    sLengthMenu: "<span>Show entries:</span> _MENU_",
                    oPaginate: {"sFirst": "First", "sLast": "Last", "sNext": ">", "sPrevious": "<"}
                }
            });
            oTable.fnSetFilteringDelay(2000);
        },
        onInit  :   function(){
            this.createtable();
        },
        onAddItem:function(){
            this.onEditItem(0);
        },
        onEditItem:function(Id){
            if (PENDING)
                return false;
            $('#dialog-edit-panel>.base-dialog-modal').html('<div class="base_loading" style="display: block;height: 60px"></div>');
            bckdialog({
                title:'Staff',
                message:$('#dialog-edit-panel'),
                buttons: [
                    {
                        text    :'Done',
                        class   :'base_button base_green base-small-border-radius',
                        click   : function() {
                            _SO.onSave();
                        }
                    },
                    {
                        text    :'Close',
                        class   :'base_button base_red base-small-border-radius',
                        click   : function() {
                            $(this).dialog("close");
                        }
                    }
                ]
            }).open();
            backend({
                url         :   _CONFIG.EDITPANEL_URI,
                data        :   {
                    Id  :   Id
                },
                callback    :   function(rsdata){
                    if(rsdata.result<0){
                        $('#dialog-edit-panel>.base-dialog-modal').html('<span style="color:red">'+rsdata.message+'</span>');
                    }else{
                        $('#dialog-edit-panel>.base-dialog-modal').html(rsdata.htmlreponse);
                        $('#dialog-edit-panel').dialog('close').dialog('open'); 
                    }
                }
            }).call();
        },
        onSave:function(){
            if( $('#_form_edit_panel').validationEngine('validate') === false)return false;
            var Params=$('#_form_edit_panel').serializeObject();
            $("#dialog-edit-panel").parents('.ui-dialog').find('.dialog-pending').show();
            new backend({
                url         :   _CONFIG.COMMIT_URI,
                data        :   {
                    Params  :   Params,
                    Id      :   $('#staf_id').val()
                },
                callback    :   function(rsdata){
                    $("#dialog-edit-panel").parents('.ui-dialog').find('.dialog-pending').hide();
                    if(rsdata.result<0){
                        bckdialog({type:'error'}).open(rsdata.message);
                    }else{
                        _SO.onRefresh();
                        _SO.onCancel();
                    }
                    
                }
            }).call();
            return false;
        },
        onCancel:function(){
            $('#dialog-edit-panel').dialog('close');
        },
        onDeleteItem:function(Id){
            backend_delete(_CONFIG.DELETE_URI,Id,function(){
                _SO.onRefresh();
            });
        },
        onRefresh:function(){
            oTable.fnDraw(false);
        },
        onFillter:function(v){
            oTable.fnFilter(v, 2);
        },
        onStatusChange:function(that,Id){
            var _status = $(that).is(':checked')?'true':'false';
            new backend({
                url         :   _CONFIG.COMMIT_URI,
                data        :   {
                    Params  :   {
                        staf_status:_status
                    },
                    Id      :   Id
                },
                callback    :   function(rsdata){
                    if(rsdata.result<0){
                        bckdialog({type:'error'}).open(rsdata.message);
                    }
                }
            }).call();
        }
    };
}());